<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>评论列表</title>
    <%- include common %>
</head>

<body>
    <%- include("head",{title:"mylist",username}) %>
    <table class="table">
        <thead>
            <tr style="color: rgb(255, 230, 0); font-size: 20px">
                <th scope="col">序号</th>
                <th scope="col">电影</th>
                <th scope="col">描述</th>
                <th scope="col">评论日期</th>
                <th scope="col">影评人</th>
                <th scope="col">评论内容</th>
                <th scope="col">操作</th>
            </tr>
        </thead>
        <tbody>
            <% result.forEach((item,index)=>{ %>
            <tr>
                <td style="width: 100px"><%- index+1 %></td>
                <td style="width: 500px">
                    <%- item.movie.original_title%> / <%- item.movie.title %>
                </td>
                <td>
                    <img style="width: 100px; height: 100px" src="<%- item.movie.images.large.replace(/img7/,'img3') %>"
                        alt="" />
                </td>
                <td><%- moment(item.time).format('YYYY-MM-DD, h:mm:ss'); %></td>
                <td><%- item.username %></td>
                <td><%- item.content %></td>
                <td>
                    <button onclick="delaction('<%- item._id %>')" data-toggle="modal"
                        <%- username==item.username?'':'disabled' %> data-target="#delbox" class="btn btn-danger"
                        onclick="">删除</button>
                </td>
                <td>
                    <% if(username==item.username){ %>
                    <button data-toggle="modal" data-target="#updatebox" data-item='<%- JSON.stringify(item) %>'
                        onclick='getupdateitem(this)' class="btn btn-success">修改</button>
                    <% }  %>
                </td>
            </tr>
            <% }) %>
            <tr>
                <td colspan="4">
                    <nav aria-label="Page navigation">
                        <ul class="pagination">
                            <li>
                                <a href="/comment/mylist?page=<%- page-1 %>" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            <li class="<%- page-2<1?'disabled':'' %>">
                                <a href="/comment/mylist?page=<%- page-2 %>"><%- page-2 %></a>
                            </li>
                            <li class="<%- page-1<1?'disabled':'' %>">
                                <a href="/comment/mylist?page=<%- page-1 %>"><%- page-1 %></a>
                            </li>
                            <li class="active">
                                <a href="/comment/mylist?page=<%- page %>"><%- page %></a>
                            </li>
                            <li class="<%- page+1>totalPage?'disabled':'' %>">
                                <a href="/comment/mylist?page=<%- page+1 %>"><%- page+1 %></a>
                            </li>
                            <li class="<%- page+2>totalPage?'disabled':'' %>">
                                <a href="/comment/mylist?page=<%- page+2 %>"><%- page+2 %></a>
                            </li>
                            <li class="<%- page+2>totalPage?'display:none':'' %>">
                                <a href="/comment/mylist?page=<%- page+1 %>" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        </ul>
                    </nav>
                </td>
                <td colspan="4">
                    <li>第 <%- page %> 页 / 共 <%- totalPage %> 页</li>
                    <li>每页 <%- pageSize %> 页 / 共 <%- total %> 条</li>
                </td>
            </tr>
        </tbody>
    </table>

    <div class="modal fade" tabindex="-1" role="dialog" id="delbox">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">删除警告</h4>
                </div>
                <div class="modal-body">
                    <p>你确定要删除这条评论吗&hellip;</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-danger" onclick="delone()">确定</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

    <div class="modal fade" tabindex="-1" role="dialog" id="updatebox">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">修改评论 </h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="title">评论标题:</label>
                        <input type="text" class="form-control" id="title" placeholder="评论标题">
                    </div>
                    <div class="form-group">
                        <label for="content">评论内容:</label>
                        <textarea type="text" class="form-control" id="content" rows="10" placeholder="评论内容"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-success" onclick="updateone()">修改</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    <%- include foot %>
</body>

</html>
<script>
    var _id = null
    function delaction(delid) {
        _id = delid
    }

    //ajax
    function delone() {
        console.log(_id)
        $.ajax({
            url: "/comment/delone",
            type: "GET",
            data: {
                id: _id
            },
            success: res => {
                if (res.type) {
                    window.location.reload()
                } else {
                    console.log("cuowu")
                }
            }
        })
    }

    function getupdateitem(that) {
        var item = JSON.parse(that.getAttribute("data-item"));
        $("#title").val(item.title)
        $("#content").val(item.content)
        _id = item._id;
    }
    function updateone() {
        $.ajax({
            url: "/comment/updateone",
            method: "POST",
            data: {
                id: _id,
                title: $("#title").val(),
                content: $("#content").val()
            },
            success: res => {
                if (res.type) {
                    window.location.reload()
                }
            }
        })
    }
</script>